﻿// See https://aka.ms/new-console-template for more information

using Autofac;
using Autofac.Extensions.DependencyInjection;
using DiffuseEnjoyIdle.Core.Extensions;
using DiffuseEnjoyIdle.Core.Helper;
using DiffuseEnjoyIdle.EntityFrameworkCore;
using DiffuseEnjoyIdle.Extension.Extensions;
using DiffuseEnjoyIdle.Model.Config;
using DiffuseEnjoyIdle.RabbitMQ;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory(a =>
{
    a.RegisterModule(new AutofacModuleRegister());
})).ConfigureAppConfiguration((hostingContext, config) =>
{
    var rootPath = hostingContext.HostingEnvironment.ContentRootPath;
    var cfgPath = PathTool.GetConfigPath(rootPath);
    config.AddCommandLine(args)
            .AddCommandLine(new[] { $"{Path.GetDirectoryName(cfgPath)}" })
            .AddJsonFile(Path.Combine(cfgPath, "appsettings.json"), false, true)
            .AddApolloExtension();
});

var services = builder.Services;
var configuration = builder.Configuration;

services.Configure<RabbitMQConfig>(configuration.GetSection("RabbitMQConfig"));

services.AddDbContext<DBContext>(
       opt => opt.UseMySql(configuration.GetConnectionString("mysql"), ServerVersion.Parse("8.0.21-mysql"))
   );
services.AddMQHandler();
services.AddHostedService<MQWorker>();


services.AddSingleton(p =>
{
    var connStr = configuration.GetConnectionString("redis");
    return ConnectionMultiplexer.Connect(connStr);
});

var app = builder.Build();

app.Run();